import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'logic.dart';
import 'order_food/logic.dart';

class AppPage extends StatelessWidget {
  final AppLogic logic = Get.put(AppLogic());
  final PageController _pageController = PageController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _pageController,
        onPageChanged: logic.changePage,
        physics: NeverScrollableScrollPhysics(),
        children: logic.pages,
      ),
      bottomNavigationBar: Obx(() => _buildBottomNavigationBar(context)),
    );
  }

  Widget _buildBottomNavigationBar(BuildContext context) {
    // 使用首页背景颜色 - 浅金色
    final Color backgroundColor = Color(0xFFBE9650);
    final Color selectedItemColor = Colors.white;
    final Color unselectedItemColor = Colors.white70;

    return Container(
      decoration: BoxDecoration(
        boxShadow: [
          BoxShadow(
            color: Colors.black12,
            blurRadius: 4,
            spreadRadius: 1,
          ),
        ],
      ),
      child: BottomNavigationBar(
        currentIndex: logic.currentIndex.value,
        onTap: (index) {
          _pageController.animateToPage(
            index,
            duration: const Duration(milliseconds: 300),
            curve: Curves.easeInOut,
          );

          // 如果是学生用户并点击了点餐页面
          if (logic.userType.value == 'STUDENT' && index == 1) {
            // 获取OrderFoodLogic实例，调用刷新方法
            final orderFoodLogic = Get.find<OrderFoodLogic>();
            orderFoodLogic.loadMenuData();
          }
        },
        type: BottomNavigationBarType.fixed,
        backgroundColor: backgroundColor,
        selectedItemColor: selectedItemColor,
        unselectedItemColor: unselectedItemColor,
        selectedLabelStyle: TextStyle(fontWeight: FontWeight.bold),
        elevation: 8,
        items: logic.getNavigationItems(),
      ),
    );
  }
}